package com.example.casper.Experiment2024.Activity;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.casper.Experiment2024.Database.PunchRecord;
import com.example.casper.Experiment2024.R;

import java.util.List;

public class PunchRecordAdapter extends RecyclerView.Adapter<PunchRecordAdapter.ViewHolder> {
    private List<PunchRecord> punchRecords;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onViewClick(PunchRecord record);
        void onDeleteClick(PunchRecord record);
    }

    public PunchRecordAdapter(List<PunchRecord> punchRecords, OnItemClickListener listener) {
        this.punchRecords = punchRecords;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_punch_record, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PunchRecord record = punchRecords.get(position);
        holder.descriptionTextView.setText(record.getDescription());
        holder.viewButton.setOnClickListener(v -> listener.onViewClick(record));
        holder.deleteButton.setOnClickListener(v -> listener.onDeleteClick(record));
    }

    @Override
    public int getItemCount() {
        return punchRecords.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView descriptionTextView;
        public Button viewButton;
        public Button deleteButton;

        public ViewHolder(View itemView) {
            super(itemView);
            descriptionTextView = itemView.findViewById(R.id.description_text);
            viewButton = itemView.findViewById(R.id.view_button);
            deleteButton = itemView.findViewById(R.id.delete_button);
        }
    }
}